﻿// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RejectController.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the RejectController type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace UnityWeb.Controllers
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web.Mvc;

  using Entities;

  using Exceptions;
  using Logging;
  using ServiceInterfaces;
  using UnityWeb.Filters;
  using UnityWeb.Models.Rejection;
  using UnityWeb.Models.Shared;

  [AuthorizeLoggedInUser]
  public class RejectController : BaseController
  {
    private readonly IRejectionService rejectionService;
    private readonly IDocumentService documentService;

    public RejectController(IRejectionService rejectionService, IDocumentService documentService, ILogger logger)
      : base(logger)
    {
      this.rejectionService = rejectionService;
      this.documentService = documentService;
    }

    [HttpPost]
    public ActionResult Document(RejectDocumentsViewModel rejectDocumentsViewModel)
    {
      var documentsAlreadyApproved = new List<String>();
      var documentsRejected = new List<String>();
      var documentsAlreadyRejcted = new List<String>();

      foreach (var document in rejectDocumentsViewModel.RejectDocumentViewModel.Where(d => d.Selected))
      {
        this.RejectDocument(document.DocumentId, documentsAlreadyApproved, documentsRejected, documentsAlreadyRejcted);
      }

      var documentWarningsViewModel = new DocumentWarningsViewModel
                                        {
                                          DocumentsAlreadyApproved = string.Join(",", documentsAlreadyApproved.ToArray()),
                                          DocumentsRejected = documentsRejected.Count,
                                          DocumentsAlreadyRejected = string.Join(",", documentsAlreadyRejcted.ToArray())
                                        };

      return this.PartialView("_DocumentWarnings", documentWarningsViewModel);
    }

    [HttpPost]
    public ActionResult Grid(String grid)
    {
      String userName = this.HttpContext.User.Identity.Name;

      BatchApprovalResults rejectionResults = this.rejectionService.RejectGrid(userName, grid);

      var documentWarningsViewModel = new DocumentWarningsViewModel
      {
        DocumentsRejected = rejectionResults.ApprovedDocuments.Count,
        DocumentsAlreadyApproved = String.Join(",", rejectionResults.AlreadyApprovedDocuments),
        DocumentsAlreadyRejected = String.Join(",", rejectionResults.AlreadyRejectedDocuments)
      };

      return this.PartialView("_DocumentWarnings", documentWarningsViewModel);
    }

    [HttpPost]
    public ActionResult Basket(RejectDocumentsViewModel rejectDocumentsViewModel)
    {
      String userName = this.HttpContext.User.Identity.Name;
      String cartId = CartItem.GetCartId(this.HttpContext);

      BatchApprovalResults rejectionResults = this.rejectionService.RejectBasket(userName, cartId);

      var documentWarningsViewModel = new DocumentWarningsViewModel
      {
        DocumentsRejected = rejectionResults.ApprovedDocuments.Count,
        DocumentsAlreadyApproved = String.Join(",", rejectionResults.AlreadyApprovedDocuments),
        DocumentsAlreadyRejected = String.Join(",", rejectionResults.AlreadyRejectedDocuments)
      };

      return this.PartialView("_DocumentWarnings", documentWarningsViewModel);
    }

    private void RejectDocument(String documentId, List<String> documentsAlreadyApproved, List<String> documentsRejected, List<String> documentsAlreadyRejcted)
    {
      try
      {
        this.rejectionService.RejectDocument(this.HttpContext.User.Identity.Name, documentId);
        documentsRejected.Add(documentId);
      }
      catch (DocumentAlreadyApprovedException)
      {
        documentsAlreadyApproved.Add(documentId);
      }
      catch (DocumentAlreadyRejectedException)
      {
        documentsAlreadyRejcted.Add(documentId);
      }
    }
  }
}
